/*
 *  $Id: JellyfishSwarm.cs 1067 2010-05-21 11:00:43Z thenn.erannor $
 *	This file is a part of Ragima CCG.
 *	(C) 2008 Ragima development group.
 *
 *	Licensed under the terms of the GNU GPL License version 2.
 *
 */

using System;

namespace Ragima.Cards001.Water.Spells {
    public class JellyfishSwarm: SpellCard {
        public JellyfishSwarm(): base("001_w_s_jellyfish_swarm") {
            Level = CompleteLevel.ReadyForTest;
            Element = ElementType.Water;
            Value = 100;
            AddManaCost(ElementType.Water, 2);
        }

        public override void DoAction(TargetArgs args) {
            args.Player.AddAffect<JellyfishSwarmAffect>();
        }

        private class JellyfishSwarmAffect: SpellAffect<JellyfishSwarm> {
            public JellyfishSwarmAffect(): base() {
                Add(new JellyfishSwarmAura());
            }
        }

        private class JellyfishSwarmAura: CreatureAuraAffect {
            private int _count;

            public JellyfishSwarmAura(): base(true, true) {
                _count = 0;
            }

            public override string Id {
                get { return AffectManager.GetCardId<JellyfishSwarm>("aura"); }
            }
            public override ElementType Element {
                get { return ElementType.Water; }
            }

            public override void SetAffect() {
                _count = GetJellyfishCount();
                base.SetAffect();
            }

            protected override void OnAddObject(AbstractObject obj) {
                base.OnAddObject(obj);
                DoUpdate();
            }
            protected override void OnRemoveObject(AbstractObject obj) {
                base.OnRemoveObject(obj);
                DoUpdate();
            }

            private void DoUpdate() {
                int count = GetJellyfishCount();
                if (_count != count) {
                    _count = count;
                    UpdateAura(true);
                }
            }

            protected override bool AcceptCreature(Creature cr) {
                return IsJellyfish(cr.Card);
            }

            protected override Affect CreateSubAffect() {
                int v = Math.Max(7 - _count, 1);
                return new JellyfishReincarnation(v);
            }

            private int GetJellyfishCount() {
                Player player = Owner.Owner;
                return GetJellyfishCount(player) + GetJellyfishCount(player.Enemy);
            }
            private int GetJellyfishCount(Player player) {
                int count = 0;
                foreach (Creature cr in player.GetCreatures())
                    if (IsJellyfish(cr.Card))
                        count++;
                return count;
            }

            /// <summary>
            /// Проверка, является ли карта существа медузой.
            /// </summary>
            private static bool IsJellyfish(CreatureCard card) {
                return card.Race.IsRace<Races.Jellyfish>();
            }
        }

        private class JellyfishReincarnation: SpellAffect<JellyfishSwarm> {
            private int _value;

            public JellyfishReincarnation(int value): base(true) {
                _value = value;
                Add(new Affects.Reincarnation(value));
            }

            public override string DescriptionText {
                get { return Description.GetText(_value); }
            }

            public override Affect Clone() {
                return new JellyfishReincarnation(_value);
            }
        }
    }
}
